<?php

class Inscrits_model extends CI_Model {
	
	protected $table;

	function __construct(){
		parent::__construct();
		$this->table = 'inscrits';
	}

	
	
	function insert($key , $post){
		$data = $post;
		if($data['num_spi'] == "")
			$data['num_spi'] = $data['num_voile'];
		$data['cle_inscription'] = $key;
		$this->db->insert($this->table, $data);
	}

	function get_inscrits($state = null, $cat = null){
		$res = ($state === null) ? 	$this->db->get($this->table) :
									(($cat === null) ? 	$this->db->get_where($this->table, array('etat'=>$state)) : 
														$this->db->get_where($this->table, array('etat'=>$state, 'categorie_flotte'=>$cat)));
		if($res->num_rows() != 0)
			return $res->result_array();
		else
			return array();
	}

	function get_inscrit_by_id($id){
		$res = $this->db->get_where($this->table, array('id_inscrit'=>$id));
		if($res->num_rows() == 1){
			$res = $res->result_array();
			return $res[0];
		}
			
		else
			return array();
	}

	function valid($id, $num){
		$this->db->where(array('id_inscrit'=>$id))
			->update($this->table, array('etat'=>1, 'num_cagnard'=>$num));
	}

	function remove($id){
		$this->db->delete($this->table, array('id_inscrit'=>$id));
	}
	
	function switch_categorie($id){
		$inscrit = $this->get_inscrit_by_id($id);
		if($inscrit['categorie_flotte'] == 1)
			$this->db->where('id_inscrit', $id)
			->update($this->table, array('categorie_flotte'=>2));
		else
			$this->db->where('id_inscrit', $id)
			->update($this->table, array('categorie_flotte'=>1));
	}
}
?>